﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TLCApp.StkForm;

namespace TLCApp
{
    public partial class stk_scrkcx : Form
    {
        string mrkfs = "1";

        string mkflx = "0";        // --0成品库, 1半成品 2,配件,3 材料
        public stk_scrkcx(string rkfs, string kflx)
        {
            InitializeComponent();
            mrkfs = rkfs;
            mkflx = kflx;
        }

       public bool _Readonlyvisable = true; ///true显示 false 不显示功能按钮
        private void btnAdd_Click(object sender, EventArgs e)
        {
            stk_scrk frmrk = new stk_scrk("");
            frmrk.stype = "0";
            frmrk.rklx = mrkfs;
            
           
            frmrk.kflx = mkflx;
            if (mrkfs != "1")
            {
                frmrk.txthtbh.Enabled = false;
               
            }
            frmrk.ShowDialog();
            btnQuery.PerformClick();
        }
        void setform( bool blflag)
        {
            btnAdd.Visible = blflag;

            btnUpdate.Visible = blflag;
        }
        
        private void btnQuery_Click(object sender, EventArgs e)
        {
            query(dtps.Text, dtpend.Text, txthtbh.Text.Trim(), mkflx);
        }

        void query(string kssj, string  jssj ,string htbh,string strtype)
        {

            dgvlist.DataSource = Dal.stk.STK_CP_RKD_QUERY(kssj, jssj, htbh, strtype, mrkfs).Tables[0];
        
        }
        void setcontrol()
        {
            dgvlist.AutoGenerateColumns = false;
            this.dgvlist.AllowUserToAddRows = false;
            this.dgvlist.MultiSelect = false;
            this.dgvlist.ReadOnly = true;
            this.dgvlist.RowHeadersWidth = 10;
            this.dgvlist.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
        }

        private void stk_scrkcx_Load(object sender, EventArgs e)
        {
            dtps.Value = DateTime.Now.AddMonths(-1);
            dtpend.Value = DateTime.Now;
            setcontrol();
            setform(_Readonlyvisable);

            btnQuery.PerformClick();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (dgvlist.RowCount > 0)
            {

                stk_scrk frmrk = new stk_scrk(dgvlist.CurrentRow.Cells["colapp"].Value.ToString());
                frmrk.stype = "1";
                frmrk.rklx = mrkfs;
                frmrk.kflx = mkflx;
                if (dgvlist.CurrentRow.Cells["colzt"].Value.ToString() == "已审核")
                {

                    frmrk.mendable = false;
                }
                else
                {
                    if (!_Readonlyvisable)
                    {
                        frmrk.mendable = false ;//不是真不显示
                    }
                    else
                    {
                        frmrk.mendable = true;
                    }
                    
                }
              
                if (mrkfs!="1" )
                {
                 frmrk.txthtbh.Enabled =false ;
                }
                frmrk.ShowDialog();
                btnQuery.PerformClick();
              
            }
        }

        private void dgvlist_DoubleClick(object sender, EventArgs e)
        {
            if (dgvlist.RowCount > 0)
            {

                stk_scrk frmrk = new stk_scrk(dgvlist.CurrentRow.Cells["colapp"].Value.ToString());
                frmrk.stype = "1";
                if (dgvlist.CurrentRow.Cells["colzt"].Value.ToString() == "已审核")
                {
                    frmrk.mendable = false;
                }
                else
                {
                    if (!_Readonlyvisable)
                    {
                        frmrk.mendable = false;//不是真不显示
                    }
                    else
                    {
                        frmrk.mendable = true;
                    }
                }
                frmrk.rklx = mrkfs;
                if (mrkfs != "1")
                {
                    frmrk.txthtbh.Enabled = false;
                }
                frmrk.kflx = mkflx;
                frmrk.ShowDialog();
                btnQuery.PerformClick();

            }
        }

        private void btncheck_Click(object sender, EventArgs e)
        {
            if (dgvlist.RowCount > 0)
            {

                string appno =dgvlist.CurrentRow.Cells["colapp"].Value.ToString();
                if (dgvlist.CurrentRow.Cells["colzt"].Value.ToString() == "未审核" && dgvlist.CurrentRow.Cells["colpddh"].Value.ToString().Trim() == "")
                {
                    DataSet dsResult = Dal.stk.STK_CP_RKD_CHECK(appno, "1", pulic.UserIfo.userId);
                    if (dsResult.Tables[0].Rows[0]["rst"].ToString().ToUpper() == "T")
                    {
                        TLCApp.pulic.Cls_Popup.MsgBox("审核成功！");

                        btnQuery.PerformClick();

                    }
                    else
                    {
                        TLCApp.pulic.Cls_Popup.MsgBox(dsResult.Tables[0].Rows[0]["msg"].ToString());
                    }
                }
                else
                {

                    TLCApp.pulic.Cls_Popup.MsgBox("只能审核未审核的单据！");
                }
             

            }
        }

        private void btnQX_Click(object sender, EventArgs e)
        {
            if (dgvlist.RowCount > 0)
            {


                string appno = dgvlist.CurrentRow.Cells["colapp"].Value.ToString();

                if (dgvlist.CurrentRow.Cells["colzt"].Value.ToString() == "已审核" && dgvlist.CurrentRow.Cells["colpddh"].Value.ToString().Trim() == "")
                {
                    DataSet dsResult = Dal.stk.STK_CP_RKD_CHECK(appno, "0", pulic.UserIfo.userId);
                    if (dsResult.Tables[0].Rows[0]["rst"].ToString().ToUpper() == "T")
                    {
                        TLCApp.pulic.Cls_Popup.MsgBox("取消成功！");

                        btnQuery.PerformClick();

                    }
                    else
                    {
                        TLCApp.pulic.Cls_Popup.MsgBox(dsResult.Tables[0].Rows[0]["msg"].ToString());
                    }
                }
                else
                {

                    TLCApp.pulic.Cls_Popup.MsgBox("只能取消已审核未盘点的单据！");
                }

            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (dgvlist.RowCount > 0)
            {


                string appno = dgvlist.CurrentRow.Cells["colapp"].Value.ToString();

                if (dgvlist.CurrentRow.Cells["colzt"].Value.ToString() == "未审核" && dgvlist.CurrentRow.Cells["colpddh"].Value.ToString().Trim() == "")
                {
                    DataSet dsResult = Dal.stk.STK_CP_RKD_CHECK(appno, "3", pulic.UserIfo.userId);
                    if (dsResult.Tables[0].Rows[0]["rst"].ToString().ToUpper() == "T")
                    {
                        TLCApp.pulic.Cls_Popup.MsgBox("作废成功！");

                        btnQuery.PerformClick();

                    }
                    else
                    {
                        TLCApp.pulic.Cls_Popup.MsgBox(dsResult.Tables[0].Rows[0]["msg"].ToString());
                    }
                }
                else
                {

                    TLCApp.pulic.Cls_Popup.MsgBox("只能作废未审核的单据");
                }

            }
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnprint_Click(object sender, EventArgs e)
        {
            if (dgvlist.RowCount > 0)
            {


                string appno = dgvlist.CurrentRow.Cells["colapp"].Value.ToString();
                try
                {
                    DataSet dsrept = Dal.stk.STK_CP_RKD_PRINT(appno);


                    //dsrept.Tables[0].TableName = "STK_RKD";
                    //dsrept.Tables[1].TableName = "STK_RKDMX";
                    string strpath = Application.StartupPath + "\\Report\\成品入库单.rpt";
                    List<string> lparam = new List<string>();
                    lparam.Add("rptname|" + pulic.UserIfo.rptname);
                    lparam.Add("rptname1|入库单");
                    PrintForm frmprint = new PrintForm(dsrept, strpath, lparam);
                    frmprint.ShowDialog();
                }
                catch (Exception ex)
                {
                    pulic.Cls_Popup.MsgErrBox(ex.ToString());
                }
            }
        }

        private void dgvlist_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
